
package com.hexinfo.dmpro.demo.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hexinfo.dmpro.demo.dao.GenFormConfMapper;
import com.hexinfo.dmpro.demo.dao.GenTableColumnMapper;
import com.hexinfo.dmpro.demo.dao.GeneratorMapper;
import com.hexinfo.dmpro.demo.dto.ColumnEntity;
import com.hexinfo.dmpro.demo.dto.GenConfig;
import com.hexinfo.dmpro.demo.dto.GenFormConf;
import com.hexinfo.dmpro.demo.dto.QueryTable;
import com.hexinfo.dmpro.demo.service.GeneratorService;
import com.hexinfo.dmpro.demo.util.GenUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * @author yemw
 * @date 2018-07-30
 * <p>
 * 代码生成器
 */
@Service
@AllArgsConstructor
public class GeneratorServiceImpl implements GeneratorService {

	private final GenTableColumnMapper tableColumnMapper;

	private final GeneratorMapper generatorMapper;

	private final GenFormConfMapper genFormConfMapper;

	/**
	 * 分页查询表
	 * @param tableName 查询条件
	 * @param dsName
	 * @return
	 */
	@Override
	public IPage<List<Map<String, Object>>> getPage(Page page, String tableName, String dsName) {
		return generatorMapper.queryList(page, tableName);
	}

	@Override
	public Map<String, String> previewCode(GenConfig genConfig) {
		// 根据tableName 查询最新的表单配置
		List<GenFormConf> formConfList = genFormConfMapper.selectList(Wrappers.<GenFormConf>lambdaQuery()
				.eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime));

//		DynamicDataSourceContextHolder.push(genConfig.getDsName());

		String tableNames = genConfig.getTableName();
		for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
			// 查询表信息
			QueryTable table = generatorMapper.queryTable(tableName, genConfig.getDsName());
			// 查询列信息
			List<ColumnEntity> columns = tableColumnMapper.selectTableColumn(tableName,
					genConfig.getDsName());
			// 生成代码
			if (CollUtil.isNotEmpty(formConfList)) {
				return GenUtils.generatorCode(genConfig, table, columns, null, formConfList.get(0));
			}
			else {
				return GenUtils.generatorCode(genConfig, table, columns, null, null);
			}
		}
		return MapUtil.empty();
	}

	/**
	 * 生成代码
	 * @param genConfig 生成配置
	 * @return
	 */
	@Override
	public byte[] generatorCode(GenConfig genConfig) {
		// 根据tableName 查询最新的表单配置
//		List<GenFormConf> formConfList = genFormConfMapper.selectList(Wrappers.<GenFormConf>lambdaQuery()
//				.eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime));

//		DynamicDataSourceContextHolder.push(genConfig.getDsName());
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(outputStream);

		String tableNames = genConfig.getTableName();
		for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
			QueryTable table = null;
			List<ColumnEntity> columns = null;
			if(StringUtils.isNotBlank(genConfig.getDsName())){
				if("oracle".equals(genConfig.getDsName().toLowerCase())){
					// 查询表信息
					table = generatorMapper.queryTableOracle(tableName, genConfig.getDsName());
					// 查询列信息
					columns = tableColumnMapper.selectTableColumnOracle(tableName, genConfig.getDsName());

				}else if ("mysql".equals(genConfig.getDsName().toLowerCase())){
					// 查询表信息
					table = generatorMapper.queryTable(tableName, genConfig.getDsName());
					// 查询列信息
					columns = tableColumnMapper.selectTableColumn(tableName, genConfig.getDsName());
				}
				// 生成代码
//			if (CollUtil.isNotEmpty(formConfList)) {
//				GenUtils.generatorCode(genConfig, table, columns, zip, formConfList.get(0));
//			}
//			else {
				GenUtils.generatorCode(genConfig, table, columns, zip, null);
//			}
			}
			IoUtil.close(zip);
		}
		return outputStream.toByteArray();
	}

}
